package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.LineBaseBigTypeDao;
import com.sailfish.springbootdemo.dao.db1.LineBaseSmallTypeDao;
import com.sailfish.springbootdemo.dao.db1.PcbaProcessDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.LineBaseSmallType;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LineBaseSmallTypeServiceImpl implements LineBaseSmallTypeService {

    @Autowired
    private LineBaseSmallTypeDao lineBaseSmallTypeDao;

    @Autowired
    private LineBaseBigTypeDao lineBaseBigTypeDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private PcbaProcessDao pcbaProcessDao;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "基线整线项目小类";

    @Override
    public List<LineBaseSmallType> getAllLineBaseSmallTypeByBigType(String searchValue, Integer lineBaseBigTypeId) {
        List<LineBaseSmallType> list = lineBaseSmallTypeDao.getAllLineBaseSmallTypeByBigType(searchValue, lineBaseBigTypeId);
        for (LineBaseSmallType lineBaseSmallType : list) {
            lineBaseSmallType.setLineBaseBigType(lineBaseBigTypeDao.getLineBaseBigTypeById(lineBaseSmallType.getLineBaseBigTypeId()));
            lineBaseSmallType.setPcbaProcess(pcbaProcessDao.getPcbaProcessById(lineBaseSmallType.getPcbaProcessId()));
            lineBaseSmallType.setPcbaProcessSmt(pcbaProcessDao.getPcbaProcessById(lineBaseSmallType.getPcbaProcessIdSmt()));
        }
        return list;
    }

    @Override
    public Result addOrUpdateLineBaseSmallType(LineBaseSmallType lineBaseSmallType, String mode) {
        try {
            if (lineBaseSmallType == null) {
                return ResultUtil.error(500, "缺少参数lineBaseSmallType", "Miss Param lineBaseSmallType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (lineBaseSmallType.getLineBaseSmallTypeId() == null || "".equals(lineBaseSmallType.getLineBaseSmallTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数lineBaseSmallTypeId", "Miss Param lineBaseSmallTypeId", null);
                }

                List<LineBaseSmallType> lineBaseSmallTypeList = lineBaseSmallTypeDao.judgeUniqueLineBaseSmallType(lineBaseSmallType.getLineBaseBigTypeId(), lineBaseSmallType.getLineBaseSmallTypeId(), lineBaseSmallType.getLineBaseSmallTypeName());
                if (lineBaseSmallTypeList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                LineBaseSmallType oldLineBaseSmallType = lineBaseSmallTypeDao.getLineBaseSmallTypeById(lineBaseSmallType.getLineBaseSmallTypeId());

                lineBaseSmallType.setUpdateTime(new Date());
                Integer integer = lineBaseSmallTypeDao.updateLineBaseSmallType(lineBaseSmallType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldLineBaseSmallType), JSONObject.toJSONString(lineBaseSmallType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                List<LineBaseSmallType> lineBaseSmallTypeByNameAndBigType = lineBaseSmallTypeDao.getLineBaseSmallTypeByNameAndBigType(lineBaseSmallType.getLineBaseSmallTypeName(), lineBaseSmallType.getLineBaseBigTypeId());
                if (lineBaseSmallTypeByNameAndBigType.size() > 0) {
                    return ResultUtil.error(500, "该类型已存在", "Already Exist", null);
                }

                lineBaseSmallType.setCreateTime(new Date());
                Integer integer = lineBaseSmallTypeDao.addLineBaseSmallType(lineBaseSmallType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(lineBaseSmallType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result deleteLineBaseSmallType(Integer lineBaseSmallTypeId) {
        try {
            if (lineBaseSmallTypeId == null || "".equals(lineBaseSmallTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数lineBaseSmallTypeId", "Miss Param lineBaseSmallTypeId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            LineBaseSmallType oldLineBaseSmallType = lineBaseSmallTypeDao.getLineBaseSmallTypeById(lineBaseSmallTypeId);

            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteLineBaseSmallType(lineBaseSmallTypeId);
            if (stringIntegerMap.get("baseCapacityRuleNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("baseCapacityRuleNum") + "处关联基线整线能力规则",
                        "Forbidden Delete", null);
            }
            Integer integer = lineBaseSmallTypeDao.deleteLineBaseSmallType(lineBaseSmallTypeId);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldLineBaseSmallType), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
